
package com.zbdx.service.impl;

import com.zbdx.dao.UserDao;
import com.zbdx.dao.impl.UserDaoImpl;
import com.zbdx.domain.User;
import com.zbdx.service.UserService;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String username, String password) throws SQLException {
        return userDao.findUserByUsernameAndPassword(username, password);
    }

    @Override
    public boolean register(User user) throws SQLException {
        return userDao.saveUser(user);
    }

    @Override
    public boolean userExists(String username) throws SQLException{
        return userDao.Exists(username);
    }

    @Override
    public User findUserByUsername(String username) {
        try {
            return userDao.findByUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean updateUserInfo(User user) {
        try {
            return userDao.updateUserInfo(user) > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    //修改
    @Override
    public boolean addUser(User user) throws SQLException {
        int res = userDao.insert(user);
        if(res > 0){
            return true;
        }else return false;
    }

    @Override
    public boolean deleteById(Integer id) throws SQLException {
        int res = userDao.deleteById(id);
        if(res > 0){
            return true;
        }else return false;
    }

    @Override
    public boolean update(User user) throws SQLException {
        int res = userDao.update(user);
        if(res > 0){
            return true;
        }else return false;
    }

    @Override
    public boolean bathDeleteUser(List<Integer> ids) throws SQLException {
        int res = userDao.bathDeleteUser(ids);
        if(res > 0){
            return true;
        }else return false;
    }

    @Override
    public User selectById(int id) throws SQLException {
        User user = userDao.selectById(id);
        return user;
    }

    @Override
    public List<User> findAll() throws SQLException {
        List<User> users = userDao.selectAll();
        return users;
    }

    @Override
    public List<User> findBysearch(String name) throws SQLException {
        List<User> users = userDao.selectBySearch(name);
        return users;
    }
}


